package com.web.cms.doc.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.cms.doc.exception.DocCommentException;
import com.web.cms.doc.facade.DocCommentFacade;
import com.web.cms.doc.message.DocCommentRequest;
import com.web.cms.doc.message.DocCommentResponse;
import com.web.cms.doc.model.DocComment;
import com.web.cms.doc.service.DocCommentService;
import com.web.cms.doc.util.DocCommentConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("cmsDocCommentProvider")
public class DocCommentProvider implements DocCommentFacade
{
	@Autowired
	private DocCommentService docCommentService;
	
	public DocCommentProvider()
	{
	}

	@Override
	public DocCommentResponse add(DocCommentRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		DocComment comment = DocCommentConvertor.toDocComment(request);
		comment = docCommentService.save(comment);
		DocCommentResponse response = DocCommentConvertor.toResponse(comment);
		return response;
	}

	@Override
	public DocCommentResponse update(DocCommentRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		DocComment comment = DocCommentConvertor.toDocComment(request);
		comment = docCommentService.save(comment);
		DocCommentResponse response = DocCommentConvertor.toResponse(comment);
		return response;
	}

	@Override
	public DocCommentResponse get(Long id)
	{
		if (id == null)
			throw new DocCommentException("文档评论ID不能为空");
		
		DocComment comment = docCommentService.get(id);
		DocCommentResponse response = DocCommentConvertor.toResponse(comment);
		return response;
	}

	@Override
	public Page<DocCommentResponse> listPage(DocCommentRequest request, int start, int size)
	{
		DocComment comment = DocCommentConvertor.toDocComment(request);
		Page<DocComment> page = new Page<>(size);
		page.setCurPage(start);
		page = docCommentService.list(comment, page);
		Page<DocCommentResponse> responsePage = DocCommentConvertor.toResponse(page);
		return responsePage;
	}
}
